package com.example.repeatsubmit.policy.conretepolicy;

import com.alibaba.fastjson.JSON;
import com.example.repeatsubmit.exception.RepeatSubmitException;
import com.example.repeatsubmit.policy.RepeatPolicy;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * @author caoqianhuan
 * @date 2022/8/19
 * @Description: 判断同一个session同一个地址和参数的的两个请求的间隔时间是否相同
 */
public class TimeInnerRefuse extends RepeatPolicy {
    public TimeInnerRefuse () {

    }
    @Override
    public boolean isRepeatSubmit(HttpServletRequest request) throws RepeatSubmitException {
        String lastRequest = getLastRequest(request);
        HttpSession session = request.getSession();
        LocalDateTime preTime = (LocalDateTime) session.getAttribute(lastRequest);
        if (preTime == null || Duration.between(preTime, LocalDateTime.now()).toMillis() > interval) {
            session.setAttribute(lastRequest, LocalDateTime.now());
            return false;
        }
        return true;
    }
}
